#include "HuffmanTree.h"

using namespace std;

int main()
{
	int n;
	cin >> n;
	int* num = (int*)calloc(n, sizeof(int));
	if (NULL == num)
	{
		perror("calloc fail");
		return -1;
	}
	for (int i = 0; i < n; i++)
		cin >> num[i];
	shellSort(num, n);
	HTree hfTree;
	HTreeInit(&hfTree, n);
	CreateHTreeNode(&hfTree, num, n);
	HTNode* root = CreateHTree(&hfTree);
	int wpl = CalculateHTree(root, 0);
	cout << wpl << endl;
	HTreeDestroy(&hfTree);

	return 0;
}